package com.itheima.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import com.itheima.utils.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@RestController
@RequestMapping("validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("checkCode")
    public Result checkCode(@RequestParam String checkCode, HttpSession session){
        String checkcode_server = (String)session.getAttribute(VerifyCodeUtils.PIC_VERIFY_CODE);
        if(checkcode_server.equalsIgnoreCase(checkCode)){
            return new Result(true, "验证通过");
        }else {
            return new Result(false, "图形验证码错误");
        }
    }

    @RequestMapping("send2Telephone")
    public Result send2Telephone(@RequestParam("telephone")String telephone){

        try {
            //生成验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();

            //通过工具类将验证码发送到指定的手机
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code);

            //设置一个key
            String key = telephone+ RedisMessageConstant.SENDTYPE_ORDER;

            //将验证码保存到redis中，并且设置10分钟的生存时间
            jedisPool.getResource().setex(key,60*10,code);

            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

    }

    /**
     * 发送登陆功能的验证码
     */
    @RequestMapping("send2Login")
    public Result send2Login(@RequestParam("telephone")String telephone){
        try {
            //生成验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();

            //发送验证码给指定的手机
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code);
            //设置一个key
            String key = telephone+RedisMessageConstant.SENDTYPE_LOGIN;
            //将验证码存入到redis中
            jedisPool.getResource().setex(key,60*10,code);

            return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (ClientException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
    /**
     * 获取验证码
     * @param session
     * @param response
     */
    @RequestMapping("getCheckCode")
    public void getCheckCode(HttpSession session, HttpServletResponse response) {
        //服务器通知浏览器不要缓存
        response.setHeader("pragma","no-cache");
        response.setHeader("cache-control","no-cache");
        response.setHeader("expires","0");

        //生成四位随机字符
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        //将验证码保存到session域中
        session.setAttribute(VerifyCodeUtils.PIC_VERIFY_CODE,verifyCode);
        int width=100;
        int height=40;
        try {
            VerifyCodeUtils.outputImage(width,height,response.getOutputStream(),verifyCode);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
